package com.example.myapplication.DataBase;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.Toolbar;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.example.myapplication.ArrayAdapterInclude.IntentArrayListAdapter;
import com.example.myapplication.Person;
import com.example.myapplication.R;
import com.google.android.material.textfield.TextInputLayout;
import java.util.ArrayList;

public class LiteActivity extends AppCompatActivity {
  PersonDataBase personDataHelper;
  EditText nameText;
  EditText jobText;
  ListView infoView;
  TextInputLayout nameLayout;
  TextInputLayout jobLayout;
  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.database_lite_layout);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setTitle("SqlLite测试");
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    InitValues();
    SetButton();
  }

  private void InitValues() {
    personDataHelper = new PersonDataBase(this);
    nameText = (EditText) findViewById(R.id.name_text);
    jobText = (EditText) findViewById(R.id.job_text);
    SetListView();
    nameLayout = (TextInputLayout) findViewById(R.id.name_layout);
    jobLayout = (TextInputLayout) findViewById(R.id.job_layout);
  }

  private void SetButton() {
    Button saveButton = (Button) findViewById(R.id.save_button);
    Button infoButton = (Button) findViewById(R.id.info_button);
    saveButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        SaveDataBySql();
      }
    });

    infoButton.setOnClickListener(new View.OnClickListener() {
      @Override public void onClick(View v) {
        ShowSqlData();
      }
    });
  }

  private void ShowSqlData() {
    personDataHelper.open();
    ArrayList<Person> personArrayList=personDataHelper.ShowAllData();
    if (personArrayList.size() == 0) {
      Toast.makeText(this, "数据库里好像还没有数据", Toast.LENGTH_SHORT);
      return;
    }
    IntentArrayListAdapter adapter = new IntentArrayListAdapter(this, personArrayList,personDataHelper);
    infoView.setAdapter(adapter);
    infoView.setVisibility(View.VISIBLE);
    personDataHelper.close();
  }

  private void SaveDataBySql() {


    String nameStr= String.valueOf(nameText.getText());
    String jobStr = String.valueOf(jobText.getText());
    boolean flag=false;
    if (nameStr.isEmpty()) {
      nameLayout.setError("输入不能为空");
      flag=true;
    }

    if (jobStr.isEmpty()) {
      jobLayout.setError("输入不能为空");
      flag=true;
    }

    if (flag) {
      Toast.makeText(this, "请检查您的输入数据", Toast.LENGTH_SHORT);
      return;
    } else {
      jobLayout.setError(null);
      nameLayout.setError(null);
    }

    personDataHelper.open();
    personDataHelper.InsertSingleData(nameStr,jobStr);
    personDataHelper.close();
    Toast.makeText(this, "保存成功", Toast.LENGTH_SHORT);

  }

  @Override public boolean onOptionsItemSelected(@NonNull MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        GoBack();
    }
    return true;
  }

  private void GoBack() {
    finish();
  }

  private void SetListView() {
    infoView = (ListView) findViewById(R.id.info_list);
    infoView.setVisibility(View.INVISIBLE);



  }


}
